// packageX/district/detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    footshow: true,
    foot_type: '',
    districtId: '',
    districtInfo: {
      
    },
    activities: [
      
    ],
    memberActivities:[],
    // 申请入驻弹窗相关
    showApplyPopup: false,
    formData: {
      name: '',
      mobile: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.id) {
      this.setData({
        districtId: options.id
      });
      this.visit();
      // 这里可以根据districtId请求商圈详情数据
      this.getData();
      this.getActivity();
    }
  },

  onChange(event) {
    if(event.detail.name == 0) {
      this.getActivity();
    }else{
      this.getMemberActivity();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '共享商圈 - ' + this.data.districtInfo.name,
      path: '/packageX/district_detail/detail?id=' + this.data.districtId
    };
  },

  visit() {
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-circle.increase-visit");
    app._getNetWork({
      url: urlStr,
      data: {
        circle_id: this.data.districtId
      },
      success: function(resdata) {
        
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-circle.circle-detail");
    app._getNetWork({
      url: urlStr,
      data: {
        circle_id: this.data.districtId
      },
      success: function(resdata) {
        console.log(resdata);
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'districtInfo': res.data,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  getActivity() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-activity.get-activity-list");
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1,
        pageSize: 100,
        commerce_circle_id: this.data.districtId
      },
      success: function(resdata) {
        console.log(resdata);
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'activities': res.data.list,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMemberActivity() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-activity.get-activity-list");
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1,
        pageSize: 100,
        commerce_circle_id: this.data.districtId
      },
      success: function(resdata) {
        console.log(resdata);
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            'activities': res.data.list,
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  onMemberTap(e) {
    wx.navigateTo({
      url: `/packageX/district_member/member?id=`+this.data.districtId,

    });
  },

  onCreateTap(e) {
    wx.navigateTo({
      url: `/packageX/activity_create/activity_create?id=`+this.data.districtId,
    });
  },

  /**
   * 显示购物车底部导航
   */
  showcarrfoot() {
    // 实现显示购物车底部导航的逻辑
  },

  /**
   * 显示IPhone适配的购物车底部导航
   */
  showcarrfoot_ipx() {
    // 实现显示IPhone适配的购物车底部导航的逻辑
  },

  /**
   * 申请入驻按钮点击事件
   */
  onApplyClick() {
    this.setData({
      showApplyPopup: true
    });
  },
  
  /**
   * 关闭申请入驻弹窗
   */
  closeApplyPopup() {
    this.setData({
      showApplyPopup: false
    });
  },
  
  /**
   * 处理表单输入
   */
  onInputChange(e) {
    console.log(e);
    const { field } = e.currentTarget.dataset;
    const value  = e.detail;
    
    this.setData({
      [`formData.${field}`]: value
    });
  },
  
  /**
   * 提交申请入驻表单
   */
  submitApply() {
    const { name, mobile } = this.data.formData;
    const { districtId } = this.data;
    
    // 表单验证
    if (!name) {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名',
        duration: 1500
      });
      return;
    }
    
    if (!mobile) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号码',
        duration: 1500
      });
      return;
    }
    
    if (!/^1[3-9]\d{9}$/.test(mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号码',
        duration: 1500
      });
      return;
    }
    
    // 提交表单数据到服务器
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.join-circle");
    app._getNetWork({
      url: urlStr,
      data: {
        circle_id: districtId,
        name: name,
        mobile: mobile
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: '申请成功',
            icon: 'success',
            duration: 2000,
            success: function() {
              // 关闭弹窗并重置表单
              that.closeApplyPopup();
              that.setData({
                formData: {
                  name: '',
                  mobile: ''
                }
              });
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg || '申请失败，请重试',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
        wx.showToast({
          icon: 'none',
          title: '网络异常，请重试',
          duration: 1500
        });
      }
    });
  },

  onDetailClick(e) {
    const {id} = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageC/Myshaky/enrollEnter/enrollEnter?id=' + id,
    });
  },

  /**
   * 功能导航点击事件
   */
  onNavItemClick(e) {
    const index = e.currentTarget.dataset.index;
    // 处理不同导航项的点击事件
    switch(index) {
    case 1: // 会员列表
      wx.navigateTo({
        url: '/packageX/district/member?id=' + this.data.districtId
      });
      break;
    case 2: // 商圈活动
      // 切换到活动标签
      break;
    case 3: // 发布活动
      // wx.navigateTo({
      //   url: '/packageX/district/create?id=' + this.data.districtId + '&type=activity'
      // });
      break;
    }
  },

  /**
   * 返回首页点击事件
   */
  onClickIcon() {
    wx.switchTab({
      url: '/packageG/index/index'
    });
  }
});